<?php

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
use think\facade\Request;
use think\Db;
// 应用公共文件
function mailto($to,$title,$content)
{
	$mail = new PHPMailer(true);                              // Passing `true` enables exceptions
try {
    //Server settings
    $mail->SMTPDebug = 0;                       // Enable verbose debug output
    $mail->CharSet = 'utf-8';           //b
    $mail->isSMTP();                                      // Set mailer to use SMTP
    $mail->Host = 'smtp.aliyun.com';  // Specify main and backup SMTP servers
    $mail->SMTPAuth = true;                               // Enable SMTP authentication
    $mail->Username = 'cn_tao@aliyun.com';                 // SMTP username
    $mail->Password = 'zqt15120088572';                           // SMTP password
    $mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
    $mail->Port = 25;                                    // TCP port to connect to

    //Recipients
    $mail->setFrom('cn_tao@aliyun.com', 'Mailer');
    $mail->addAddress($to);     // Add a recipient
    //$mail->addAddress('ellen@example.com');               // Name is optional
    //$mail->addReplyTo('info@example.com', 'Information');
    //$mail->addCC('cc@example.com');
    //$mail->addBCC('bcc@example.com');

    //Attachments
    //$mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
    //$mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name

    //Content
    $mail->isHTML(true);                                  // Set email format to HTML
    $mail->Subject = $title;
    $mail->Body    = $content;
    //$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';


   return $mail->send();
    echo 'Message has been sent';
	} catch (Exception $e) {
    echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
    }
}

//根据用户主键ID，查询用户名称
if(!function_exists('getUserName'))
{
    function getUserName($user_id)
    {
       return Db::name('user')->where('id',$user_id)->value('name');
    }
}

if(!function_exists('getUserImg'))
{
    function getUserImg($user_id)
    {
       return Db::name('user')->where('id',$user_id)->value('user_img');
    }
}


//根据文章分类ID查询文章名
function getCateName($cate_ename)
    {
       return Db::name('cate')->where('ename',$cate_ename)->value('catename');
    }


//过滤文章摘要
function getArtContent($content)
{
    return mb_substr(strip_tags($content),0,50).'更多详情...';
}



//根据帖子收藏主键ID，查询帖子名称
if(!function_exists('getArticName'))
{
    function getArticName($article_id)
    {
       return Db::name('article')->where('id',$article_id)->value('title');
    }
}


//根据用户名查询主页
function jump()
{
    $username = Request::param('username');
    return Db::name('user')->where('name',$username)->find();
}

